<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>面试题</title>
</head>
<body>

</body>
<script>
    /*
       1.输入：nums = [1,2,3,4]
       输出：[1,3,6,10]
       解释：计算过程为 [1, 1+2, 1+2+3, 1+2+3+4] 。
    */
    var nums = [1, 2, 3, 4];

    function fn1(arr) {
        var newArr = [];
        for (var i = 0; i < arr.length; i++) {
            if (i == 0) {
                newArr[0] = nums[0];
            } else {
                newArr[i] = nums[i] + newArr[i - 1];
            }

        }
        console.log(newArr);
        return newArr;
    };
    fn1(nums);

    /*
        2. 输入：n = 234
        输出：15
        解释：
        各位数之积 = 2 * 3 * 4 = 24
        各位数之和 = 2 + 3 + 4 = 9
        结果 = 24 - 9 = 15
    */

    function fn2(num) {
        var arr = [];
        var str = num;
        for (var i = 0; i < num.toString().length; i++) {
            if (str.toString().length != 1) {
                arr[i] = str % 10;
            } else {
                arr[i] = str;
            }
            str = Math.floor(str / 10);
        }
        var product = arr.reduce(function (pre, next) {
            return pre * next;
        });
        var sum = arr.reduce(function (pre, next) {
            return pre + next;
        });
        var result = product - sum;
        console.log(result);
        return result;
    }

    fn2(234);

    /*
        3. 输入：s = "goodjob", indices = [4,5,6,7,0,1,2,3]
        输出："goodjob"
        解释：如图所示，"goodjob" 重新排列后变为 "jobgood" 。}
    */
    function fn3(str,indices) {
        var newStr = '';
        for (var i = 0; i < indices.length; i++) {
            newStr += str.charAt(indices[i]);
        }
        console.log(newStr);
        return newStr;
    }
    fn3("goodjob",[4,5,6,7,0,1,2,3]);

</script>
</html>